<template>
  <!-- 品牌  -->
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a @click="changeBrand(brand.id)" :class="{ active: filterData.selectedBrand === brand.id }" href="javasript:;"
          v-for="brand in filterData.brands" :key="brand.id">{{ brand.name }}</a>
      </div>
    </div>
    <!-- 分类 -->
    <div class="item" v-for="p in filterData.saleProperties" :key="p.id">
      <div class="head">{{ p.name }}：</div>
      <div class="body">
        <a @click="changeAttr(p, attr.id)" :class="{ active: p.selectedProp === attr.id }" href="javasript:;"
          v-for="attr in p.properties" :key="attr.id">{{ attr.name }}</a>
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>
<script lang="ts" setup>
import { ref, watch } from 'vue';
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '../../api/category'
let emit = defineEmits(['fliterChange'])
const route = useRoute()
// 控制每次更新骨架的展示
const filterLoading = ref(false)
// 筛选区后台数据
const filterData: any = ref([])
// 根据路由Id的监听变化来发起数据请求
watch(() => route.params.id, (newVal) => {
  // 对数据处理
  // 每个选项加上全部选项
  if (newVal && `/category/sub/${route.params.id}` === route.path) {
    findSubCategoryFilter(route.params.id).then((data: any) => {
      // 品牌全部
      // 控制品牌选项高亮
      data.result.selectedBrand = null
      data.result.brands.unshift({ id: null, name: '全部' })
      // 销售属性全部
      data.result.saleProperties.forEach(item => {
        // 控制分类选项高亮
        item.selectedProp = null
        item.properties.unshift({ id: null, name: '全部' })
      });
      filterData.value = data.result
      filterLoading.value = false
    })
  }
}, { immediate: true })
// 整理筛选条件参数
const getFilterParams = () => {
  // { brandId: '', attrs: [{groupName:'',propertyName:''},....] }
  const obj = { brandId: null, attrs: [] }
  // 存储筛选的品牌参数
  obj.brandId = filterData.value.selectedBrand
  // 存储筛选的分类参数
  filterData.value.saleProperties.forEach(item => {
    if (item.selectedProp) {
      item.properties.find(prop => {
        prop.id === item.selectedProp
        obj.attrs.push({ groupName: item.name, propertyName: prop.name })
      })
    }
  })
  // 避免点击attrs.length === 0时发请求，浪费资源
  if (obj.attrs.length === 0) obj.attrs = null
  return obj
}
// 品牌筛选
const changeBrand = (brandId) => {
  if (filterData.value.selectedBrand === brandId) return
  filterData.value.selectedBrand = brandId
  emit('fliterChange', getFilterParams())
}
// 分类筛选
const changeAttr = (p, attrId) => {
  if (p.selectedProp === attrId) return
  p.selectedProp = attrId
  emit('fliterChange', getFilterParams())
}
</script>
 <style lang='less' scoped>
 @import '../../assets/styles/mixins.less';
 @import '../../assets/styles/variables.less';
 
 // 筛选区
 .sub-filter {
   background: #fff;
   padding: 25px;
 
   .item {
     display: flex;
     line-height: 40px;
 
     .head {
       width: 80px;
       color: #999;
     }
 
     .body {
       flex: 1;
 
       a {
         margin-right: 36px;
         transition: all .3s;
         display: inline-block;
 
         &.active,
         &:hover {
           color: @xtxColor;
         }
       }
     }
   }
 }
 </style>
